﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using IssueTrackerTest.IssueTrackerService;

namespace IssueTrackerTest
{
    /// <summary>
    /// Summary description for IssueCustomTypeValueTest
    /// </summary>
    [TestClass]
    public class IssueCustomTypeValueTest
    {
        
  
        [TestMethod]
        public void TestAddIssueCustomTypeValue()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();
            
            IssueTrackerService.IssueCustomTypeValue entity = new IssueTrackerService.IssueCustomTypeValue();

            List<IssueTrackerService.Issue> issueList;
            List<IssueTrackerService.ProjectCustomType> customTypeList;

            try
            {
                client.Open();
                issueList = client.ListIssues().ToList();
                customTypeList = client.ListProjectCustomTypes().ToList();

                if (issueList != null && customTypeList != null)
                {
                    entity.Id = System.Guid.NewGuid();
                    entity.CreatorId = "testuser";
                    entity.IssueId = issueList.First().Id;
                    entity.ProjectCustomTypeId = customTypeList.First().Id;
                    entity.Value = "test";

                    IssueTrackerService.IssueCustomTypeValue result = client.CreateIssueCustomTypeValue(entity);

                    Assert.IsNotNull(result.Id);

                }
                else
                {
                    Assert.Fail("ProjectCustomType or Issue tables empty");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

        [TestMethod]
        public void TestListByRef()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            client.Open();

            List<IssueTrackerService.Issue> issueList;

            try
            {

                issueList = client.ListIssues().ToList();

                if (issueList != null)
                {
                    List<IssueCustomTypeValue> childList = client.ListIssueCustomTypeValuesByRef(issueList.First().Id).ToList();

                    Assert.IsNotNull(childList);

                    Assert.IsTrue(childList.Count > 0);
                }

            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }

        }

        [TestMethod]
        public void TestUpdateIssueCustomTypeValue()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            List<IssueTrackerService.IssueCustomTypeValue> list;

            try
            {
                client.Open();
                list = client.ListIssueCustomTypeValues().ToList();

                if (list != null)
                {
                    IssueTrackerService.IssueCustomTypeValue entity = list.First();

                    entity.Value = "modified value";

                    IssueTrackerService.IssueCustomTypeValue result = client.UpdateIssueCustomTypeValue(entity);

                    Assert.AreEqual("modified value", result.Value);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
            
        }

        [TestMethod]
        public void TestDeleteIssueCustomTypeValue()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            System.Guid id;
            List<IssueTrackerService.IssueCustomTypeValue> list;

            try
            {
                client.Open();
                list = client.ListIssueCustomTypeValues().ToList();

                if (list != null)
                {
                    IssueTrackerService.IssueCustomTypeValue entity = list.First();

                    id = entity.Id;

                    client.DeleteIssueCustomTypeValue(entity);

                    var checkIssueCustomTypeValue = client.GetIssueCustomTypeValue(id);

                    Assert.IsNull(checkIssueCustomTypeValue);
                }
                else
                {
                    Assert.Fail("IssueCustomTypeValue Table is empty");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

   
    }
}
